package com.woniuxy.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.woniuxy.entity.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woniuxy.dao.CoachDao;
import com.woniuxy.entity.Coach;
import com.woniuxy.entity.FreeTime;
import com.woniuxy.entity.MyStudent;
import com.woniuxy.entity.Order;
import com.woniuxy.entity.PageBean;
import com.woniuxy.entity.Relationship;
import com.woniuxy.entity.Signing;
import com.woniuxy.entity.User;
import com.woniuxy.entity.Venue;
import com.woniuxy.service.CoachService;
import com.woniuxy.utils.CodeUtils;
import com.woniuxy.utils.TimeUtils;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class CoachServiceImpl implements CoachService {

	@Autowired
	private CoachDao coachDao;

	/**
	 * 通过教练id查询教练
	 */
	@Override
	public Coach findCoachByCid(int cid) {
		return coachDao.findCoachByCid(cid);
	}

	@Override
	public List<Coach> findCoach() {
		return coachDao.findCoach();
	}

	/*
	 * 注册添加教练
	 */
	@Override
	public int addCoach(Coach coach) {
		BigDecimal money = new BigDecimal(0.00);
		String nickname = "WN-" + coach.getCname();
		coach.setIsAccept("1");
		coach.setCstatus("1");

		coach.setMoney(money);
		coach.setNickname(nickname);
		coach.setAvatar("");
		String cname = coach.getCname();
		String phone = coach.getPhoneNumber();
		if (coachDao.findCoachByCname(cname) != null) {
			return 2;
		}
		if (coachDao.findCoachByPhoneNumber(phone) != null) {
			return 3;
		}
		coachDao.addCoach(coach);
		return 1;
	}

	@Override
	public Coach findCoachByCname(String cname) {
		return coachDao.findCoachByCname(cname);
	}

	@Override
	public Coach findCoachByPhoneNumber(String phone) {
		return coachDao.findCoachByPhoneNumber(phone);
	}
	/*
	 * 修改完善信息
	 */
	@Override
	public int updateCoachInformation(Coach coach ,int cid) {
		int error=coachDao.updateCoachInformation(coach,cid);
		   if (error>0){
	            //如果有条数发生改变 则返回1 修改成功
	            return  1;
	        }
	        //如果失败返回0 修改失败
	        return  0;
	}

	/**
	 * 按条件分页查询
	 *
	 * @param condition
	 *            查询条件
	 * @return
	 */
	@Override
	public PageBean<Coach> findCoachByCondition(Coach condition, int page, int pageSize) {
		//处理参数作为条件
		condition.setIndex((page-1)*pageSize);
		condition.setPageSize(pageSize);
		condition.setNickname("%"+condition.getNickname()+"%");//模糊查询条件
		//将coach作为参数放入查询方法
		//查总数
		List<Coach> coachByConditionNoPage = coachDao.findCoachByConditionNoPage(condition);
		//分页
		List<Coach> results = coachDao.findCoachByCondition(condition);
		int totalNums=coachByConditionNoPage.size();
		//将得到的结果放入pageBean中
		PageBean<Coach> coachPageBean = new PageBean<>();
		coachPageBean.setData(results);
		coachPageBean.setTotalNum(totalNums);
		coachPageBean.setTotalPage(totalNums%pageSize==0?totalNums/pageSize:totalNums/pageSize+1);
		coachPageBean.setNumsOfCurrPage(page!=coachPageBean.getTotalPage()?pageSize:totalNums%pageSize);
		return coachPageBean;
	}

	/**
	 * 根据id查教练
	 * @param cid 教练id
	 * @return 教练或null
	 */
	@Override
	public Coach findCoachById(int cid) {
		return coachDao.findCoachById(cid);
	}

	  //修改完善地址
	@Override
	public int updateAddressByCid(String address, int cid, BigDecimal longitude, BigDecimal latitude) {
		   int error=coachDao.updateAddressByCid(address,cid,longitude,latitude);
	        if (error>0){
	            //如果有条数发生改变 则返回1 修改成功
	            return  1;
	        }
	        //如果失败返回0 修改失败
	        return  0;
	}

	//添加我的学生
	@Override
	public int addMyStudent(MyStudent myStudent) {
		//先查询是否已经是我的学生
		MyStudent ms=coachDao.selectMyStudent(myStudent);
		 if (ms!=null){
	            //如果有条数发生改变 则返回2 已经是我的学生
	            return  2;
	        }
		myStudent.setSignTime(TimeUtils.getNowTime());
		int  error=coachDao.addMyStudent(myStudent);
		 if (error>0){
	            //如果有条数发生改变 则返回1 修改成功
	            return  1;
	        }
	        //如果失败返回0 修改失败
	        return  0;
	}

	/*
	 * 查询是否是我的学生
	 */
	@Override
	public int selectMyStudent(MyStudent myStudent) {
		MyStudent error=coachDao.selectMyStudent(myStudent);
		 if (error!=null){
	            //如果有条数发生改变 则返回1 修改成功
	            return  1;
	        }
	        //如果失败返回0 修改失败
	        return  0;
	}

	@Override
	public int addFreetimeByCid(int cid ,String day,String when) {
		int error=coachDao.addFreetimeByCid(cid,day,when);
		 if (error>0){
	            //如果有条数发生改变 则返回1 增加成功
	            return  1;
	        }
	        //如果失败返回0 增加失败
	        return  0;
	}
	/*
	 * 判断空闲时间是否存在
	 */
	@Override
	public int isFreetimeByCid(int cid, String day, String when) {
		FreeTime error=coachDao.isFreetimeByCid(cid,day,when);
		 if (error!=null){
	            //如果有条数发生改变 则返回1 已存在
	            return  1;
	        }
	        //如果没有返回0
	        return  0;
	}
	// 添加银行卡
	@Override
	public int insertBankCard(Coach coach) {
		// System.out.println(coachDao.findCoachByCid(coach.getCid()).getBankCard());当前数据库中已存在的银行卡号
		// System.out.println(coach.getBankCard());输入的银行卡号
		if (coach.getBankCard().equals(coachDao.findCoachByCid(coach.getCid()).getBankCard())) {
			return 2;
		}
		coachDao.insertBankCard(coach);
		return 1;
	}

	// 取现
	@Override
	public int updateMoney(Coach coach) {
		// 获取当前账户总额
		BigDecimal countmoney = findCoachByCid(coach.getCid()).getMoney();
		System.out.println(countmoney);
		// 取现的金额
		BigDecimal cashingmoney = coach.getMoney();
		System.out.println(cashingmoney);
		// 比较总额与取现金额大小
		int a = countmoney.compareTo(cashingmoney);
		if (a == -1) {
			return 2;// 余额不足
		}
		// 取现后余额更新到数据库
		if (a == 0 || a == 1) {
			BigDecimal money = countmoney.subtract(cashingmoney);
			coach.setMoney(money);
			// System.out.println("========="+coach);
			coachDao.updateMoney(coach);
		}
		return 1;
	}

	// 重置密码
	@Override
	public int updatePwd(int cid, String pwd) {
		coachDao.updatePwd(cid, pwd);
		return 1;
	}

	// 关注分页查询
	@Override
	public PageBean<Relationship> getByPage(int page, int cid) {
		PageBean<Relationship> pageBean = new PageBean<Relationship>();
		int index = (page - 1) * 8;
		List<Relationship> data = coachDao.getByPage(index, 8, cid);
		// int total = coachDao.getNums();
		// System.out.println(total);
		int totalNum = coachDao.getNumsOfRelationship(cid);
		pageBean.setData(data);
		pageBean.setTotalNum(totalNum);
		pageBean.setTotalPage(totalNum % 8 == 0 ? totalNum / 8 : totalNum / 8 + 1);
		pageBean.setNumsOfCurrPage(data.size());
		pageBean.setPage(page);
		pageBean.setPageSize(8);
		return pageBean;

	}

	// 添加关注
	@Override
	public int addRelationship(Relationship relationship) {
		coachDao.addRelationship(relationship);
		return 1;
	}

	// 签约场馆
	@Override
	public PageBean<Signing> getVenueByPage(int page, int cid) {
		PageBean<Signing> pageBean = new PageBean<Signing>();
		int index = (page - 1) * 8;
		List<Signing> data = coachDao.getVenueByPage(index, 8, cid);
		// int total =coachDao.getNums();
		// System.out.println(total);
		int totalNum = coachDao.getCountOfVenue(cid);
		pageBean.setData(data);
		pageBean.setTotalNum(totalNum);
		pageBean.setTotalPage(totalNum % 8 == 0 ? totalNum / 8 : totalNum / 8 + 1);
		pageBean.setNumsOfCurrPage(data.size());
		pageBean.setPage(page);
		pageBean.setPageSize(8);
		return pageBean;

	}

	// 学员列表
	@Override
	public PageBean<Order> getUserByPage(int page, int cid) {
		PageBean<Order> pageBean = new PageBean<Order>();
		int index = (page - 1) * 8;
		List<Order> data = coachDao.getUserByPage(index, 8, cid);
		// int total =coachDao.getNums();
		// System.out.println(total);
		int totalNum = coachDao.getCountOfUser(cid);
		pageBean.setData(data);
		pageBean.setTotalNum(totalNum);
		pageBean.setTotalPage(totalNum % 8 == 0 ? totalNum / 8 : totalNum / 8 + 1);
		pageBean.setNumsOfCurrPage(data.size());
		pageBean.setPage(page);
		pageBean.setPageSize(8);
		return pageBean;
	}

	// 更改邀约状态
	@Override
	public int changeRmStatus(int sender, int target, int reply) {
		coachDao.changeRmStatus(sender, target, reply);
		return 1;
	}

	// 判断好友
	@Override
	public int judgeRelationship(int fan, int followed) {
		int judge = coachDao.judgeRelationship(fan, followed);
		if (judge >= 1) {
			return 1;
		} else {
			return 2;
		}

	}

	//好友id  list
	@Override
	public List<Integer> friendRelationship(int cid) {
		List<Integer> data = coachDao.getRelationship(cid);
		List<Integer> finddate = new ArrayList<>();
		// 遍历数据，判断是否互关，封装成一个新的list
		Iterator<Integer> relationship = data.iterator();
		while (relationship.hasNext()) {
			// 被关注的id
			Integer integer = (Integer) relationship.next();
			// 判断是否为好友
			int id = integer;
			int judgeid = coachDao.judgeRelationship(id, cid);
			if (judgeid >= 1) {
				finddate.add(integer);
			}

		}
		return finddate;
	}
	/*
	 * 教练获取约私教报酬
	 * @author 王敞
	 */
	@Override
	public int updateOrderMoneyByOrder(Coach coach) {
		return coachDao.updateMoney(coach);
	}	

	
	 //修改头像
	@Override
	public int updateAvatarByCid(String avatar, int cid) {
		 int error=coachDao.updateAvatarByCid(avatar,cid);
	        if (error>0){
	            //如果有条数发生改变 则返回1 成功
	            return CodeUtils.UPDATE_CODE;
	        }
	        //如果失败返回10
	        return CodeUtils.UPDATEFALSE_CODE;
	}

	@Override
	public List<Coach> findCoachByName(String name) {
		/*Coach coach=new Coach();
		//Integer getid=(Integer)id;
		coach.setCname(name);
		coach.setCid(id);*/
		return coachDao.findCoachByName(name);
		
	}

	@Override
	public List<Venue> findVenueByName(String name) {
		/*Venue venue=new Venue();
		//Integer getid=(Integer)id;
		venue.setVid(id);
		venue.setVname(name);*/
		return coachDao.findVenueByName(name);
	}

	@Override
	public List<User> findUserByName(String name) {
		/*User user =new User();
		//Integer getid=(Integer)id;
		user.setUid(id);
		user.setUname(name);*/
		return coachDao.findUserByName(name);
	}

}
